//created by: Lim Yi Le
Ext.define('MajorProjectSenchaTouch.controller.LibraryUpdates', {
    extend: 'Ext.app.Controller',
    config:{
        views: ['LibraryUpdatesModule.LibraryUpdates', 'LibraryUpdatesModule.LibraryUpdatesFullview'],
        refs:{
            navView: 'mainNavView',
            libraryUpdatesPage: 'libraryupdatespage',
            libraryUpdatesFullviewPage: 'libraryupdatesfullviewpage'
            
        }
    },
     /*
|--------------------------------------------------------------------------
| displayBlogCategory (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on the submit button on the Recommend Title
| overlay to submit the form data to email.
|    
*/
    displayBlogCategory: function(selectedCategory, contentSize){
        
        //get new category of library updates to form a new url string
        var luStore = Ext.getStore('LibraryUpdates');
        var newUrl = MajorProjectSenchaTouch.app.serverUrl + 'libraryupdates/index/' + selectedCategory + contentSize;
        
        //reload library updates dataview with new proxy url
        luStore.load({
            url: newUrl
        });
    },
     /*
|--------------------------------------------------------------------------
| goFullviewPage (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function is triggered by tapping on an item in the LibraryUpdates view that is summarized.
| The application will transit to the LibraryUpdatesFullview with the full content of the blog post.
|    
*/
    goFullviewPage: function(title, fullContent, pubDate){     
        var ctrl = this;
        var previousPage = ctrl.showMask();
        
        // formats date to be similar to '31 August 2012'
        var pubDateFormat = new Date(pubDate);
        var date = Ext.Date.format(pubDateFormat, 'j F Y');
        
        var task = Ext.create('Ext.util.DelayedTask', function(){            
            ctrl.pushView('Library Updates', 'libraryupdatesfullviewpage');
            
            // formats and displays library updates data
            var itemDisplay = '<div class="productBox">' +
                                            '<div class="productDate">' +
                                                '<div class="productDateText">' + date + '</div>' +
                                            '</div>' +
                                            '<div class="productBody">' +
                                                '<h1><p align="right">' + title + '</p></h1>' +
                                                '<hr>' +
                                                '<h4>' + fullContent + '</h4>' +
                                            '</div>' +
                                        '</div>';
            var lufvPanel = Ext.getCmp('itemFullContent');
            lufvPanel.setHtml(itemDisplay);
            ctrl.hideMask(previousPage);
        });
        task.delay(1);
    },
        /*
|--------------------------------------------------------------------------
| pushView (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Set the title of the navigation bar and push the page into the navigation
| view.
|
*/ 
    pushView: function(setTitle, setXtype){
        var view = this.getNavView();
        
        view.push({
            title: setTitle,
            xtype: setXtype
        })
    },
        /*
|--------------------------------------------------------------------------
| showMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Get the current item in the navigation view and set the loading mask.
| Return the current item as previousPage.
|
*/  
    showMask: function(){
        var view = this.getNavView();
        var previousPage = view._activeItem;
        
        previousPage.setMasked({
            xtype: 'loadmask',
            message: 'Please wait...'
        });
        
        return previousPage;
    },
        /*
|--------------------------------------------------------------------------
| hideMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Hide the loading mask of the page.
|
*/ 
     hideMask: function(previousPage){        
        previousPage.setMasked(false);
    }
});

